package cn.gson.springboot.controller.salecontroller;

import cn.gson.springboot.aop.MyLog;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.service.saleservice.CtschemesService;
import cn.gson.springboot.model.vo.salesvo.RefundinfoVo;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.bcel.internal.generic.DADD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;

@Controller
public class CtschemesController {
    @Autowired
    CtschemesService ctschemesService;

    @RequestMapping("/erfund_Info")
    @ResponseBody
    public List<RefundinfoVo> RefundinfoInfo(String searchInfo){
        List<RefundinfoVo> refundinfoVos = ctschemesService.RefundinfoInfo(searchInfo);
        return refundinfoVos;
    }

    @RequestMapping("/all_ctschemsinfo")
    @ResponseBody
    public List<Ctschemes> allCtshemsInfo(String searchInfo){
        List<Ctschemes> ctschemes = ctschemesService.allCtshemsInfo(searchInfo);
        return ctschemes;
    }

    
    /**
     * 新增回款计划
     * @param request
     */
    @RequestMapping("/save_ctscheme")
    @ResponseBody
    public Integer saveCtschemes(HttpServletRequest request){
        Ctschemes ctscheme = JSON.parseObject(request.getParameter("ctscheme"), Ctschemes.class);
        ctscheme.setCtState(0);
        ctscheme.setCtThere("主题未填写");
        Integer integer = ctschemesService.saveCtschems(ctscheme);
        return integer;
    }


    /**
     * 新增发票
     * @param request
     */
    @RequestMapping("/save_invo")
    @ResponseBody
    public Integer saveOrderinvoce(HttpServletRequest request){
        Orderinvoice invo = JSON.parseObject(request.getParameter("invo"), Orderinvoice.class);
        System.err.println("xppl"+invo);
        Integer integer = ctschemesService.saveOrderinvoce(invo);
        return integer;
    }

    /**
     * 新增交付计划
     * @param deliveryplan
     * @return
     */
    @RequestMapping("/save_deliveryplan")
    @ResponseBody
    public Integer saveDeliveryplan(@RequestBody Deliveryplan deliveryplan){
        Integer integer = ctschemesService.saveDeliveryplan(deliveryplan);
        OrderInfo orderInfoByOrderId = deliveryplan.getOrderInfoByOrderId();
        Integer integer1 = ctschemesService.selectMaxDeliveryId(orderInfoByOrderId.getOrderId());
        Integer num=integer1+1;
        System.out.println("交付最大"+integer1);
        System.out.println("订单"+orderInfoByOrderId.getOrderName());
        System.err.println(deliveryplan.getDeliverydetailsByDeliveryId());
        deliveryplan.setDeThere(orderInfoByOrderId.getOrderName()+"-"+num);
        deliveryplan.setDeState(0);
        deliveryplan.setDeFhuostate(0);
        for(int i=0;i<deliveryplan.getDeliverydetailsByDeliveryId().size();i++){
            deliveryplan.getDeliverydetailsByDeliveryId().get(i).setDeliveryplan(deliveryplan);
            Integer detaNumber = deliveryplan.getDeliverydetailsByDeliveryId().get(i).getDetaNumber();
            deliveryplan.getDeliverydetailsByDeliveryId().get(i).setDetaReturnumber(detaNumber);
        }
       ctschemesService.saveDeliverydetails(deliveryplan.getDeliverydetailsByDeliveryId());
        return integer;
    }


    /**
     * 换货新增
     * @param exchange
     */
    @RequestMapping("/save_exchang")
    @ResponseBody
    public void saveExchang(@RequestBody Exchange exchange){
        exchange.setExcState(1);//可进维修
        exchange.setExcMainState(1);//可再次发货
        ctschemesService.saveExchang(exchange);
        for(int i=0;i<exchange.getExchangedetailsByExcId().size();i++){
            exchange.getExchangedetailsByExcId().get(i).setExchangeByExcId(exchange);
        }
        ctschemesService.saveExchangdata(exchange.getExchangedetailsByExcId());
    }

    /**
     * T退货新增
     * @param returnorder
     */
    @RequestMapping("/save_returnorder")
    @ResponseBody
    public void saveRouter( @RequestBody Returnorder returnorder){

        returnorder.setRetuState(1);
        Integer integer = ctschemesService.saveReturnInfo(returnorder);
        for(int index=0;index<returnorder.getReturnorderdetailsByRetuId().size();index++){
            returnorder.getReturnorderdetailsByRetuId().get(index).setReturnorderByRetuId(returnorder);
        }
        String s = returnorder.toString();
        ctschemesService.saveReturndataInfo(returnorder.getReturnorderdetailsByRetuId());

        //下单时间
        java.util.Date utilDate =new java.util.Date();
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        long time = sqlDate.getTime();
        Timestamp timestamp = new Timestamp(time);

        RefundinfoEntity ref = new RefundinfoEntity();
        ref.setRefundId(0);
        ref.setRefundOrderid( returnorder.getOrderInfoByOrderId().getOrderId());
        ref.setRefundType(1);
        ref.setRefundTime(timestamp);
        ref.setRefundCustomerid(returnorder.getRetuCustomerid());
        ref.setRefundUserid(returnorder.getRetuUsertomid());
        ref.setRefundContractid(integer);
        ref.setRefundState(1);
        ref.setRefundAmount(returnorder.getRetuYprice());
        ctschemesService.saveRefundInfo(ref);

    }



    @RequestMapping("/delivery_plan")
    @ResponseBody
    public List<Deliveryplan> deliveryplan(Integer orderId){
        List<Deliveryplan> deliveryplan = ctschemesService.deliveryplanList(orderId);
        return deliveryplan;
    }


    @RequestMapping("/ctschemes_list")
    @ResponseBody
    public List<Ctschemes> ctschemesList(Integer id){
        List<Ctschemes> ctschemes = ctschemesService.ctschemesList(id);
        return ctschemes;
    }


    /**
     * 回款确定
     * @param contractId
     * @param hkPlay
     * @param fpstate
     * @param amount
     * @param ctId
     * @param orderId
     */
    @MyLog(type = "回款",value = "新增了回款")
    @RequestMapping("/chang_ctschems")
    @ResponseBody
    public void changCtschems(Integer contractId,Integer hkPlay,Integer fpstate,Integer amount,Integer ctId,Integer orderId){
        System.err.println("金额"+amount);
        System.err.println("合同id"+contractId);
        System.err.println("付款方式id"+hkPlay);
        System.err.println("发票状态id"+fpstate);
        System.out.println("回款计划id"+ctId);
        ctschemesService.changCtschems(contractId,hkPlay,fpstate,amount,ctId,orderId);
    }

    @RequestMapping("/orderinvoice_list")
    @ResponseBody
    public List<Orderinvoice> orderinvoiceList(Integer id){
        List<Orderinvoice> orderinvoices = ctschemesService.orderinvoiceList(id);
        return orderinvoices;
    }


    /*@RequestMapping("/invoiceInfo_list")
    @ResponseBody
    public  Map<String,Object>  invoiceInfo(Integer page,Integer size,String name,String timeValue){
        System.err.println("代处理的时间"+timeValue);
        Page<Object> pageinfo= PageHelper.startPage(1,10);
        List<Deliveryplan> deliveryplans = ctschemesService.invoiceInfo();
        Map<String,Object> map = new HashMap<>()receiving_list;
        map.put("total",pageinfo.getTotal());
        map.put("rows",pageinfo.getResult());
        return map;
    }*/



    @RequestMapping("/delete_ctschems")
    @ResponseBody
    public void deleteDeliveryInfo(Integer id){
        ctschemesService.deleteDeliveryInfo(id);
    }



    @RequestMapping("/outbound_ctschems")
    @ResponseBody
    public void outboundDeliveryInfo(Integer id){
        ctschemesService.outboundDeliveryInfo(id);
    }


}
